﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.SessionState;
using System.Data;
namespace Web.FrmSetting
{
    /// <summary>
    /// frm 的摘要说明
    /// </summary>
    public class frm : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            common.IsLogin();
            if (context.Request.QueryString["type"] != null)
            {
                string strType = context.Request.QueryString["type"].ToString();
                switch (strType)
                {
                    case "BackUpDB": BackUpDB(context); break;//备份数据库
                    case "BackUpDBToClient": BackUpDBToClient(context); break;//备份数据库到客户端
                        
                    case "getUserInfo": getUserInfo(context); break;//备份数据库
                  
                }
            }

        }

        /// <summary>
        /// 由当前登陆用户的ID获取其所在的网点信息
        /// </summary>
        /// <param name="context"></param>
        void BackUpDB(HttpContext context)
        {
            string filePath = "d:\\BackUpDB\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day;
            if (!Directory.Exists("filePath"))
            {
                Directory.CreateDirectory(filePath);
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("  BACKUP DATABASE GrainBankDB");
            strSql.Append("  TO disk = '" + filePath + "\\GrainBankDB.bak'");

            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                //if (File.Exists(filePath + "\\GrainBankDB.bak"))
                //{
                //    File.SetAttributes(filePath + "\\GrainBankDB.bak", FileAttributes.ReadOnly);//设置文件为只读文件
                //}
                context.Response.Write("OK");
            }
            else
            {
                context.Response.Write("Error");
            }
        }


        ///// <summary>
        ///// 由当前登陆用户的ID获取其所在的网点信息
        ///// </summary>
        ///// <param name="context"></param>
        //void BackUpDB(HttpContext context)
        //{
        //    string strfilePath = "BackUpDB\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month+"\\"+DateTime.Now.Day;
        //    string filePath = context.Server.MapPath("~/"+strfilePath);
        //    string fullfilePath = context.Server.MapPath("~/" + strfilePath + "\\GrainBankDB.bak");
        //    if (!Directory.Exists(filePath))
        //    {
        //        Directory.CreateDirectory(filePath);
        //    }

        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("  BACKUP DATABASE GrainBankDB");
        //    strSql.Append("  TO disk = '" + fullfilePath + "'");
          
        //    if (SQLHelper.ExecuteNonQuery(strSql.ToString())!= 0)
        //    {
        //        //if (File.Exists(fullfilePath))
        //        //{
        //        //    File.SetAttributes(fullfilePath, FileAttributes.ReadOnly);//设置文件为只读文件
        //        //}
        //        context.Response.Write(fullfilePath);
        //    }
        //    else
        //    {
        //        context.Response.Write("Error");
        //    }
        //}

        void BackUpDBToClient(HttpContext context)
        {
            string strfilePath = "BackUpDB\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day;
            string filePath = context.Server.MapPath("~/" + strfilePath);
            string fullfilePath = context.Server.MapPath("~/" + strfilePath + "\\GrainBankDB.bak");
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("  BACKUP DATABASE GrainBankDB");
            strSql.Append("  TO disk = '" + fullfilePath + "'");

            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                //if (File.Exists(fullfilePath))
                //{
                //    File.SetAttributes(fullfilePath, FileAttributes.ReadOnly);//设置文件为只读文件
                //}

                //将文件下载到客户端
               //context.Response.ContentType = "application/x-zip-compressed";
               //context.Response.AddHeader("Content-Disposition", "attachment;filename=GrainBankDB.bak");
               //string filename = fullfilePath;
               // context.Response.TransmitFile(filename);

               
                FileInfo fileInfo = new FileInfo(fullfilePath);
                context.Response.Clear();
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                context.Response.AddHeader("Content-Disposition", "attachment;filename=GrainBankDB.bak");
                context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                context.Response.AddHeader("Content-Transfer-Encoding", "binary");
                context.Response.ContentType = "application/octet-stream";
                context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                context.Response.WriteFile(fileInfo.FullName);
                context.Response.Flush();
                context.Response.End();
            }
            else
            {
               
            }
        }

        void getUserInfo(HttpContext context)
        {

            string ID = context.Session["ID"].ToString();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("  SELECT A.ID, A.strLoginName,A.strRealName,B.strName AS WBID");
            strSql.Append("  FROM dbo.Users A INNER JOIN dbo.WB B ON A.WB_ID=B.ID");
            strSql.Append("  WHERE A.ID="+ID);

            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());

            if (SQLHelper.ExecuteNonQuery(strSql.ToString())!= 0)
            {
                context.Response.Write(JsonHelper.ToJson(dt));
            }
            else
            {
                context.Response.Write("Error");
            }
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}